﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class MainSceneUIChangeAnim : ViewControllerLoadAnimation
{
    //目前仅作延迟销毁用


    GraphicRaycaster m_GraphicRaycaster;
    Canvas m_canvas;

    ZUIAnimTools _zuiAnimTools;
    public int sortingOrder = 100;
    public float fadeInTime = 0.4f;
    public float fadeOutTime = 0.4f;
    
    // Use this for initialization
    void Awake () {
        m_GraphicRaycaster = GetComponent<GraphicRaycaster>();
        m_canvas = GetComponent<Canvas>();
        _zuiAnimTools = GetComponent<ZUIAnimTools>();
        if (_zuiAnimTools == null) _zuiAnimTools = gameObject.AddComponent<ZUIAnimTools>();
    }

    protected override void _PlayShowAnim(ViewController dc, Action onAnimEnd)
    {
        m_GraphicRaycaster.enabled = true;

        if (onAnimEnd != null)
            onAnimEnd();
    }
    protected override void _PlayInLoadingAnim(Action onEnd)
    {
        if (onEnd != null)
            onEnd();
    }

    protected override void _PlayHideAnim( Action onAnimEnd)
    {
        Hide(() =>
        {
            if (onAnimEnd != null)
                onAnimEnd();
        });
    }

    public void Hide(Action onFinished)
    {
        m_canvas.sortingOrder = sortingOrder;

        if (m_GraphicRaycaster == null) m_GraphicRaycaster = GetComponent<GraphicRaycaster>();


        _zuiAnimTools.UIAlpha(transform.GetComponent<RectTransform>(), 0, fadeOutTime,()=>
        {
            onFinished?.Invoke();
            if(m_GraphicRaycaster != null)
                m_GraphicRaycaster.enabled = false;

            SetBlock(false);

        }, ZUIAnimTools.EaseType.Curve);
        

    }

    void SetBlock(bool block)
    {
        if (this)
        {
            foreach (var img in GetComponentsInChildren<Image>())
            {
                img.raycastTarget = block;
            }
            foreach (var Text in GetComponentsInChildren<Text>())
            {
                Text.raycastTarget = block;
            }
        }
    }
    

    // Update is called once per frame
    void Update () {
		
	}
}
